package cn.bluethink.mouse;

import java.awt.event.MouseEvent;

import cn.bluethink.graphical.Line;
import cn.bluethink.view.GraphView;

public class MouseDrawLineOp implements IMouseOperator {

	private Line mLine = new Line();
	private boolean mLButtonDown = false;
	private GraphView mGraphView;
	
	public MouseDrawLineOp(GraphView view) {
		this.mGraphView = view;
	}
	
	@Override
	public void mouseClick(MouseEvent event) {
	}

	@Override
	public void mouseReleased(MouseEvent event) {
		if ( !mLButtonDown ){
			return;
		}
		mLine.getToPoint().setLocation(event.getPoint());
		Line newLine = new Line(mLine.getFromPoint(), mLine.getToPoint());
		mGraphView.mGraphs.add(newLine);
		mLButtonDown = false;
		mGraphView.repaint();
	}

	@Override
	public void mouseEntered(MouseEvent event) {
	}

	@Override
	public void mousePressed(MouseEvent event) {
		mLButtonDown = true;
		mLine.getFromPoint().setLocation(event.getPoint());
		mLine.getToPoint().setLocation(event.getPoint());
	}

	@Override
	public void mouseExited(MouseEvent event) {
	}

	@Override
	public void mouseDragged(MouseEvent event) {
		if ( !mLButtonDown ) {
			return;
		}

		mLine.draw(mGraphView.getGraphics());
		mLine.getToPoint().setLocation(event.getPoint());
		mGraphView.refresh();
		mLine.draw(mGraphView.getGraphics());
	}

	@Override
	public void mouseMoved(MouseEvent event) {
		if ( !mLButtonDown ) {
			return;
		}
		mLine.draw(mGraphView.getGraphics());
		mLine.getToPoint().setLocation(event.getPoint());
		mLine.draw(mGraphView.getGraphics());
	}

}
